home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Games / DroneZone / DZSpace.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  4.1 KB  |  171 lines  |  [TEXT/MPS ]

  1. /*
  2.  *    File:        DZSpace.c
  3.  *
  4.  *    Contents:    Submits the spacejunk.
  5.  *
  6.  *    Copyright © 1996 Apple Computer, Inc.
  7.  */
  8.  
  9. #include <assert.h>
  10.  
  11. #include <Quickdraw.h>
  12.  
  13. #include "QD3D.h"
  14. #include "QD3DGeometry.h"
  15. #include "QD3DShader.h"
  16. #include "QD3DView.h"
  17.  
  18. #include "DZSpace.h"
  19.  
  20.  
  21. #ifndef    SPACE_JUNK_IS_LINES
  22. #define    SPACE_JUNK_IS_LINES    1
  23. #endif
  24.  
  25. #define SPACE_INTERVAL        2.0            // Grid cell size
  26. #define SPACE_CUBE            3            // Half side of the cube of cells
  27. #define SPACE_SPREAD1        0.0001        // Mulitplier to Random for line start
  28. #define SPACE_SPREAD2        0.00002        // Mulitplier to Random for line length
  29.  
  30.  
  31. static TQ3AttributeSet    gSpaceColor            = NULL;
  32.  
  33.  
  34. /* =============================================================================
  35.  *        Space_Init (external)
  36.  *
  37.  *    Initializes the space stuff.
  38.  * ========================================================================== */
  39. void Space_Init(
  40.     void)
  41. {
  42.     gSpaceColor = Q3AttributeSet_New();
  43. }
  44.  
  45.  
  46. /* =============================================================================
  47.  *        Space_Exit (external)
  48.  *
  49.  *    Prepares for exit.
  50.  * ========================================================================== */
  51. void Space_Exit(
  52.     void)
  53. {
  54.     if (gSpaceColor != NULL)
  55.     {
  56.         Q3Object_Dispose(gSpaceColor);
  57.         gSpaceColor = NULL;
  58.     }
  59. }
  60.  
  61.  
  62. /* =============================================================================
  63.  *        Space_Submit (external)
  64.  *
  65.  *    Submits all the 3D geometry of the spacejunk.
  66.  * ========================================================================== */
  67. void Space_Submit(
  68.     TQ3ViewObject        inView,
  69.     const TQ3Point3D*    inPosition,
  70.     const TQ3Vector3D*    inDirection)
  71. {
  72.     long                x;
  73.     long                y;
  74.     long                z;
  75.     long                loX;
  76.     long                loY;
  77.     long                loZ;
  78.     long                hiX;
  79.     long                hiY;
  80.     long                hiZ;
  81.     float                px;
  82.     float                py;
  83.     float                pz;
  84.     TQ3ColorRGB            color;
  85.  
  86. #if    SPACE_JUNK_IS_LINES
  87.     TQ3LineData            lineData;
  88. #else    // !SPACE_JUNK_IS_LINES
  89.     TQ3PointData        pointData;
  90. #endif
  91.     
  92.     assert(inView != NULL);
  93.     assert(inPosition != NULL);
  94.     assert(inDirection != NULL);
  95.     
  96.     // Find the center grid cell
  97.     px = inPosition->x + inDirection->x*SPACE_INTERVAL*SPACE_CUBE;
  98.     py = inPosition->y + inDirection->y*SPACE_INTERVAL*SPACE_CUBE;
  99.     pz = inPosition->z + inDirection->z*SPACE_INTERVAL*SPACE_CUBE;
  100.     
  101.     x = (px + 0.5*SPACE_INTERVAL)/SPACE_INTERVAL;
  102.     y = (py + 0.5*SPACE_INTERVAL)/SPACE_INTERVAL;
  103.     z = (pz + 0.5*SPACE_INTERVAL)/SPACE_INTERVAL;
  104.     
  105.     // Find the range to cover
  106.     loX = x - SPACE_CUBE;
  107.     loY = y - SPACE_CUBE;
  108.     loZ = z - SPACE_CUBE;
  109.     
  110.     hiX = loX + 2*SPACE_CUBE;
  111.     hiY = loY + 2*SPACE_CUBE;
  112.     hiZ = loZ + 2*SPACE_CUBE;
  113.     
  114. #if    SPACE_JUNK_IS_LINES
  115.     // Set up the line data
  116.     lineData.vertices[0].attributeSet    = NULL;
  117.     lineData.vertices[1].attributeSet    = NULL;
  118.     lineData.lineAttributeSet            = gSpaceColor;
  119. #else    // !SPACE_JUNK_IS_LINES
  120.     pointData.pointAttributeSet            = gSpaceColor;
  121. #endif
  122.     
  123.     
  124.     // Do the cube
  125.     for (x = loX; x <= hiX; x++)
  126.     {
  127.         px = (x+0.5)*SPACE_INTERVAL;
  128.         
  129.         for (y = loY; y <= hiY; y++)
  130.         {
  131.             py = (y+0.5)*SPACE_INTERVAL;
  132.             
  133.             for (z = loZ; z <= hiZ; z++)
  134.             {
  135.                 pz = (z+0.5)*SPACE_INTERVAL;
  136.                 
  137.                 // Make Random() repeatable based on cell index
  138.                 qd.randSeed = x^y^z;
  139.                 
  140.                 // Set up the line color -- blend between purple and green
  141.                 color.b = ((unsigned short) Random()) * (1.0/65535.0);
  142.                 color.r = 0.4*color.b;
  143.                 color.g = 0.8*(1.0-color.b);
  144.                 
  145.                 Q3AttributeSet_Add(gSpaceColor, kQ3AttributeTypeDiffuseColor, &color);
  146.                 
  147. #if    SPACE_JUNK_IS_LINES
  148.                 // Set up the endpoints
  149.                 lineData.vertices[0].point.x = px + Random()*SPACE_SPREAD1;
  150.                 lineData.vertices[0].point.y = py + Random()*SPACE_SPREAD1;
  151.                 lineData.vertices[0].point.z = pz + Random()*SPACE_SPREAD1;
  152.                 
  153.                 lineData.vertices[1].point.x = lineData.vertices[0].point.x + Random()*SPACE_SPREAD2;
  154.                 lineData.vertices[1].point.y = lineData.vertices[0].point.y + Random()*SPACE_SPREAD2;
  155.                 lineData.vertices[1].point.z = lineData.vertices[0].point.z + Random()*SPACE_SPREAD2;
  156.  
  157.                 // Submit the line
  158.                 Q3Line_Submit(&lineData, inView);
  159. #else    // !SPACE_JUNK_IS_LINES
  160.                 pointData.point.x = px + Random()*SPACE_SPREAD1;
  161.                 pointData.point.y = py + Random()*SPACE_SPREAD1;
  162.                 pointData.point.z = pz + Random()*SPACE_SPREAD1;
  163.                 Q3Point_Submit(&pointData, inView);
  164. #endif            
  165.             }
  166.         }
  167.     }
  168. }
  169.  
  170.  
  171.